package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.*;
import org.apache.ibatis.annotations.*;
import java.util.Date;
import java.util.List;

@Mapper
public interface MockExamRecordMapper {

    @Select("SELECT " +
            "exam_id AS examId, " +
            "user_id AS userId, " +
            "username, " +
            "exam_type AS examType, " +
            "exam_date AS examDate, " +
            "total_questions AS totalQuestions, " +
            "correct_questions AS correctQuestions, " +
            "total_time AS totalTime, " +
            "score, " +
            "created_at AS createdAt " +
            "FROM mock_exams " +
            "WHERE user_id = #{userId} " +
            "ORDER BY exam_date DESC " +
            "LIMIT #{offset}, #{size}")
    List<MockExam> getMockExamRecordsByUserId(
            @Param("userId") Long userId,
            @Param("offset") int offset,
            @Param("size") int size
    );

    @Select("<script>" +
            "SELECT " +
            "exam_id AS examId, " +
            "user_id AS userId, " +
            "username, " +
            "exam_type AS examType, " +
            "exam_date AS examDate, " +
            "total_questions AS totalQuestions, " +
            "correct_questions AS correctQuestions, " +
            "total_time AS totalTime, " +
            "score, " +
            "exam_strategy AS examStrategy, " +  // 添加这一行
            "created_at AS createdAt " +
            "FROM mock_exams " +
            "WHERE user_id = #{userId} " +
            "<if test='startDate != null and endDate != null'>" +
            "AND exam_date BETWEEN #{startDate} AND #{endDate} " +
            "</if>" +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND exam_type LIKE CONCAT('%', #{keyword}, '%') " +
            "</if>" +
            "ORDER BY exam_date DESC " +
            "LIMIT #{offset}, #{size}" +
            "</script>")
    List<MockExam> searchMockExamRecords(
            @Param("userId") Long userId,
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate,
            @Param("keyword") String keyword,
            @Param("offset") int offset,
            @Param("size") int size
    );

    @Select("SELECT " +
            "exam_id AS examId, " +
            "user_id AS userId, " +
            "username, " +
            "exam_type AS examType, " +
            "exam_date AS examDate, " +
            "total_questions AS totalQuestions, " +
            "correct_questions AS correctQuestions, " +
            "total_time AS totalTime, " +
            "score, " +
            "created_at AS createdAt " +
            "FROM mock_exams WHERE exam_id = #{examId}")
    MockExam getMockExamRecordById(@Param("examId") Long examId);

    @Select("SELECT " +
            "eq.exam_id AS examId, " +
            "eq.question_id AS questionId, " +
            "eq.question_type AS questionType, " +
            "eq.sequence AS sequence, " +
            "eq.user_answer AS userAnswer, " +
            "eq.is_correct AS isCorrect " +
            "FROM exam_questions eq " +
            "WHERE eq.exam_id = #{examId} " +
            "ORDER BY eq.sequence")
    List<ExamQuestion> getExamQuestionsByExamId(@Param("examId") Long examId);

    @Select("SELECT q.* FROM questions q " +
            "JOIN exam_questions eq ON q.question_id = eq.question_id " +
            "WHERE eq.exam_id = #{examId} " +
            "ORDER BY eq.sequence")
    List<QuestionInfo> getQuestionsByExamId(@Param("examId") Long examId);

    @Select("SELECT cq.* FROM choice_questions cq " +
            "JOIN exam_questions eq ON cq.question_id = eq.question_id " +
            "WHERE eq.exam_id = #{examId} AND eq.question_type LIKE 'single_choice%'")
    List<ChoiceQuestion> getChoiceQuestionsByExamId(@Param("examId") Long examId);

    @Select("SELECT fq.* FROM fill_blank_questions fq " +
            "JOIN exam_questions eq ON fq.question_id = eq.question_id " +
            "WHERE eq.exam_id = #{examId} AND eq.question_type = 'fill_in_the_blank'")
    List<FillBlankQuestion> getFillBlankQuestionsByExamId(@Param("examId") Long examId);

    @Select("SELECT sq.* FROM subjective_questions sq " +
            "JOIN exam_questions eq ON sq.question_id = eq.question_id " +
            "WHERE eq.exam_id = #{examId} AND eq.question_type = 'subjective'")
    List<SubjectiveQuestion> getSubjectiveQuestionsByExamId(@Param("examId") Long examId);

}